// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.material.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.material.controller.request.QueryMaterialStatusReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.material.controller.request.ImportMaterialStatusChangeReq;
import com.liuxinlong.modules.material.controller.request.QueryMaterialReq;
import com.liuxinlong.modules.material.controller.request.UpdateMaterialStatusReq;
import com.liuxinlong.modules.material.service.MaterialStatusService;

import javax.servlet.http.HttpServletResponse;

/**
 * 物料状态变更控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月17日
 */
@RestController
@RequestMapping("material/status")
public class MaterialStatusController {
    @Autowired
    private MaterialStatusService materialStatusService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询物料状态变更记录
     *
     * @param req 请求信息
     * @return 物料状态变更记录
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MATERIAL_STATUS, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageMaterialStatusRecord(@RequestBody QueryMaterialReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("materialNo", req.getMaterialNo());
        List<Map<String, Object>> dataList = materialStatusService.pageMaterialStatusRecord(queryMap);
        int totalNum = materialStatusService.getStatusRecordCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询物料状态变更记录成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 批量变更物料状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("batch-update")
    @LogAnnotation(value = "批量变更物料状态", business = LogBusinessConstants.MATERIAL_STATUS, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp batchUpdateMaterialStatus(@RequestBody UpdateMaterialStatusReq req) {
        materialStatusService.batchUpdateMaterialStatus(req.getTargetIds(), req.getRegion(), req.getTargetStatus());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("批量变更物料状态成功");
        return resp;
    }

    /**
     * 删除物料状态变更记录
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除物料状态变更记录", business = LogBusinessConstants.MATERIAL_STATUS, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteMaterialStatus(@RequestBody BaseDeleteReq req) {
        materialStatusService.deleteMaterialStatus(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除物料状态变更记录成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 导入数据
     * @return 返回数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入物料状态变更记录", business = LogBusinessConstants.MATERIAL_STATUS, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importDetails(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = materialStatusService.importDetails((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入变更记录详情信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入物料状态变更记录", business = LogBusinessConstants.MATERIAL_STATUS, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddDetail(@RequestBody ImportMaterialStatusChangeReq req) {
        return materialStatusService.batchAddDetail(req.getDataList());
    }

    /**
     * 导出物料状态变更信息
     *
     * @param req 请求体
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出物料状态变更信息", business = LogBusinessConstants.MATERIAL_STATUS, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportMaterialStatus(@RequestBody QueryMaterialStatusReq req, HttpServletResponse response) {
        materialStatusService.exportMaterialStatus(req.getStartTime(), response);
    }
}
